package com.ganinfo.common.redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import com.ganinfo.common.constant.PropertiesConstant;

/**
 * @ClassName: RedisUtils
 * @Description: Redis操作接口
 * @author: ruikanwang
 * @date: 2017年2月24日 下午3:00:12
 */
public class RedisUtils {
	private static JedisPool pool = null;
	private static ThreadLocal<JedisPool> poolThreadLocal = new ThreadLocal<JedisPool>();

    /**
     * @Title: getPool
     * @Description: 构建redis连接池
     * @return
     * @return: JedisPool
     */
	public static JedisPool getPool() {
		String env = PropertiesConstant.BTSQL_EXT_PRO.getStr("PRODUCT_MODE");//当前环境测试还是正式
		env="false".equals(env)? "test":"formal";
		if (pool == null) {
			JedisPoolConfig config = new JedisPoolConfig();
			config.setMaxActive(Integer.valueOf(PropertiesConstant.CONFIG_PRO.getStr("redis.pool.maxActive")));
			config.setMaxIdle(Integer.valueOf(PropertiesConstant.CONFIG_PRO.getStr("redis.pool.maxIdle")));
			config.setMaxWait(Long.valueOf(PropertiesConstant.CONFIG_PRO.getStr("redis.pool.maxWait")));
			config.setTestOnBorrow(Boolean.valueOf(PropertiesConstant.CONFIG_PRO.getStr("redis.pool.testOnBorrow")));
			config.setTestOnReturn(Boolean.valueOf(PropertiesConstant.CONFIG_PRO.getStr("redis.pool.testOnReturn")));
			pool = new JedisPool(config, PropertiesConstant.CONFIG_PRO.getStr(env+".redis.ip"),
					Integer.valueOf(PropertiesConstant.CONFIG_PRO.getStr(env+".redis.port")),
					100000, PropertiesConstant.CONFIG_PRO.getStr(env+".redis.pool.password"));
		}
		return pool;
	}

	/**
	 * @Title: getConnection
	 * @Description: 获取连接
	 * @return
	 * @return: JedisPool
	 */
	public static JedisPool getConnection() {
		// ②如果poolThreadLocal没有本线程对应的JedisPool创建一个新的JedisPool，将其保存到线程本地变量中。
		if (poolThreadLocal.get() == null) {
			pool = RedisUtils.getPool();
			poolThreadLocal.set(pool);
			return pool;
		} else {
			return poolThreadLocal.get();// ③直接返回线程本地变量
		}
	}

	/**
	 * @Title: returnResource
	 * @Description: 返还到连接池
	 * @param pool
	 * @param redis
	 * @return: void
	 */
	public static void returnResource(JedisPool pool, Jedis redis) {
		if (redis != null) {
			pool.returnResource(redis);
		}
	}

	/**
	 * @Title: get
	 * @Description: 获取数据
	 * @param key
	 * @return
	 * @return: String
	 */
	public static String get(String key) {
		String value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * @Title: get
	 * @Description: 获取数据
	 * @param key
	 * @return
	 * @return: byte[]
	 */
	public static byte[] get(byte[] key) {
		byte[] value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.get(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * @Title: del
	 * @Description: 删除数据
	 * @param key
	 * @return
	 * @return: Long
	 */
	public static Long del(String key) {
		Long value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * @Title: del
	 * @Description: 删除数据
	 * @param key
	 * @return
	 * @return: Long
	 */
	public static Long del(byte[] key) {
		Long value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.del(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * @Title: exists
	 * @Description: 判断是否存在
	 * @param key
	 * @return
	 * @return: Boolean
	 */
	public static Boolean exists(String key) {
		Boolean value = null;

		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			value = jedis.exists(key);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return value;
	}

	/**
	 * @Title: set
	 * @Description: 赋值数据
	 * @param key
	 * @param value
	 * @param expireSeconds(过期时间，秒)
	 * @return
	 * @return: Long
	 */
	public static Long set(String key, String value, int expireSeconds) {
		Long result = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			jedis.set(key, value);
			result = jedis.expire(key, expireSeconds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return result;
	}

	/**
	 * @Title: expire
	 * @Description: 设置过期时间
	 * @param key
	 * @param expireSeconds(过期时间，秒)
	 * @return
	 * @return: Long
	 */
	public static Long expire(String key, int expireSeconds) {
		Long result = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.expire(key, expireSeconds);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return result;
	}

	/**
	 * @Title: set
	 * @Description: 赋值数据
	 * @param key
	 * @param value
	 * @return
	 * @return: String
	 */
	public static String set(String key, String value) {
		String result = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.set(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return result;
	}

	/**
	 * @Title: sadd
	 * @Description: 赋值数据
	 * @param key
	 * @param value
	 * @return
	 * @return: Long
	 */
	public static Long sadd(String key, String value) {
		Long result = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.sadd(key, value);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return result;
	}

	/**
	 * @Title: sismember
	 * @Description: 判断set中是否有值
	 * @param key
	 * @param member
	 * @return
	 * @return: Boolean
	 */
	public static Boolean sismember(String key, String member) {
		Boolean result = null;
		JedisPool pool = null;
		Jedis jedis = null;
		try {
			pool = getPool();
			jedis = pool.getResource();
			result = jedis.sismember(key, member);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 释放redis对象
			pool.returnBrokenResource(jedis);
			// 返还到连接池
			returnResource(pool, jedis);
		}

		return result;
	}

}
